package com.abing.chatRoom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abing.chatRoom.common.ErrorCode;
import com.abing.chatRoom.exception.BusinessException;
import com.abing.chatRoom.model.VO.UserVO;
import com.abing.chatRoom.model.domain.Friend;
import com.abing.chatRoom.model.domain.User;
import com.abing.chatRoom.service.FriendService;
import com.abing.chatRoom.mapper.FriendMapper;
import com.abing.chatRoom.service.TeamService;
import com.abing.chatRoom.service.UserService;
import com.abing.chatRoom.service.UserTeamService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author abing
* @description 针对表【friend(好友表)】的数据库操作Service实现
* @createDate 2024-08-06 18:54:43
*/
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend>
    implements FriendService{

    @Resource
    private UserService userService;
    @Resource
    private TeamService teamService;
    @Resource
    private UserTeamService userTeamService;

    @Resource
    private FriendMapper friendMapper;

    @Override
    public List<UserVO> friendList(User loginUser) {
        if(loginUser==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long userId = loginUser.getId();
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        List<Friend> list = this.list(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<Long> idList = list.stream().map(Friend::getFriendId).collect(Collectors.toList());
        List<UserVO> userVOList = new ArrayList<>();
        idList.forEach(id->{
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userService.getById(id),userVO);
            userVOList.add(userVO);
        });
        return userVOList;
    }

    @Override
    public void friendAdd(long id, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if(loginUser==null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        long loginUserId = loginUser.getId();
        User user = userService.getById(id);
        if(user==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Friend friend = new Friend();
        friend.setUserId(loginUserId);
        friend.setFriendId(id);
        boolean save = this.save(friend);
        if(!save){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
    }

    @Override
    public boolean isFriend(Long id,  Long userId) {

        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("friendId",id);
//        Friend friend = friendMapper.selectOne(queryWrapper);
        Friend friend = this.getOne(queryWrapper);
        if(friend==null){
            return false;
        }
        return true;
    }

    @Override
    public void friendDelete(Long id, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if(loginUser==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",loginUser.getId());
        queryWrapper.eq("friendId",id);
        Friend friend = this.getOne(queryWrapper);
        Long friendId = friend.getId();
        if(!this.removeById(friendId)){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
    }



}




